package top.fangw.admin.runner;

import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import top.fangw.common.mybatis.annotation.DyDataSource;
import top.fangw.common.mybatis.util.DynamicDataSourceUtil;

/**
 * @description: 初始化动态数据源
 * @author: fgw
 * @createDate: 2024/1/19
 */
@Slf4j
@Order(value = 1) // 数字越小，越先执行
@Component
@RequiredArgsConstructor
public class InitDataSource implements CommandLineRunner {


    private final DynamicDataSourceUtil dynamicDataSourceUtil;

    @Override
    @DyDataSource
    public void run(String... args) throws Exception {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setPoolName("base_info_tenant1");
        dataSourceProperty.setUrl("jdbc:mysql://82.157.172.66:3308/base_info_tenant1?serverTimezone=Asia/Shanghai&userUnicode=true&characterEncoding=utf-8");
        dataSourceProperty.setUsername("fgw");
        dataSourceProperty.setPassword("Fgw@123");
        dataSourceProperty.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dynamicDataSourceUtil.addDataSource(dataSourceProperty);
// base_info_tencentId
        DataSourceProperty dataSourceProperty1 = new DataSourceProperty();
        dataSourceProperty1.setPoolName("base_info_tencentId");
        dataSourceProperty1.setUrl("jdbc:mysql://82.157.172.66:3308/base_info_tencentId?serverTimezone=Asia/Shanghai&userUnicode=true&characterEncoding=utf-8");
        dataSourceProperty1.setUsername("fgw");
        dataSourceProperty1.setPassword("Fgw@123");
        dataSourceProperty1.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dynamicDataSourceUtil.addDataSource(dataSourceProperty1);
        log.info("初始化动态数据源完成");
    }
}
